/*
 * The contents of this web application are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this web application except in compliance with 
 * the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 * for the specific language governing rights and limitations under the License.
 * 
 * The Original Code is owned by and the Initial Developer of the Original Code is 
 * Composite A/S (Danish business reg.no. 21744409). All Rights Reserved
 * 
 * Section 11 of the License is EXPRESSLY amended to include a provision stating 
 * that any dispute, including but not limited to disputes related to the enforcement 
 * of the License, to which Composite A/S as owner of the Original Code, as Initial 
 * Developer or in any other role, becomes a part to shall be governed by Danish law 
 * and be initiated before the Copenhagen City Court ("K�benhavns Byret")            
 */

using System;
using System.Linq;
using System.Web;
using System.Workflow.Activities;
using Composite.C1Console.Actions;
using Composite.Tools.PackageCreator.ElementProvider.EntityTokens;

namespace Composite.Tools.PackageCreator.Workflow
{
    public sealed partial class EditPackageWorkflow : Composite.C1Console.Workflow.Activities.FormsWorkflow
    {
        public EditPackageWorkflow()
        {
            InitializeComponent();
        }

        private void InitCodeActivity_ExecuteCode(object sender, EventArgs e)
        {
            var package = PackageCreatorFacade.GetPackageInformation(this.EntityToken.Source);
            if (this.BindingExist("Package") == false)
                this.Bindings.Add("Package", package);

            if (this.BindingExist("MaxCompositeVersionSupported") == false)
                this.Bindings.Add("MaxCompositeVersionSupported", package.MaxCompositeVersionSupported.ToString());

            if (this.BindingExist("MinCompositeVersionSupported") == false)
                this.Bindings.Add("MinCompositeVersionSupported", package.MinCompositeVersionSupported.ToString());

            var request = HttpContext.Current.Request;
            if (this.BindingExist("PackageUrl") == false)
            {
                var packageUrl = string.Format("{0}/Composite/InstalledPackages/services/Composite.Tools.PackageCreator/GetPackage.ashx?package={1}", request.ApplicationPath == "/" ? "" : request.ApplicationPath, this.EntityToken.Source);
                this.Bindings.Add("PackageUrl", packageUrl);
            }
            if (this.BindingExist("ConfigUrl") == false)
            {
                var packageUrl = string.Format("{0}/Composite/InstalledPackages/services/Composite.Tools.PackageCreator/GetPackage.ashx?config={1}", request.ApplicationPath == "/" ? "" : request.ApplicationPath, this.EntityToken.Source);
                this.Bindings.Add("ConfigUrl", packageUrl);
            }

        }

        private void ValidateSave(object sender, ConditionalEventArgs e)
        {
            e.Result = true;
            var package = this.GetBinding<PackageInformation>("Package");
            var maxCompositeVersionSupported = this.GetBinding<string>("MaxCompositeVersionSupported");
            var minCompositeVersionSupported = this.GetBinding<string>("MinCompositeVersionSupported");
            try
            {
                package.MaxCompositeVersionSupported = new Version(maxCompositeVersionSupported);
            }
            catch
            {
                this.ShowFieldMessage("MaxCompositeVersionSupported", PackageCreatorFacade.GetLocalization("Error.WrongVersion"));
                e.Result = false;
            }

            try
            {
                package.MinCompositeVersionSupported = new Version(minCompositeVersionSupported);
            }
            catch
            {
                this.ShowFieldMessage("MinCompositeVersionSupported", PackageCreatorFacade.GetLocalization("Error.WrongVersion"));
                e.Result = false;
            }

            if (string.IsNullOrEmpty(package.Description))
            {
                this.ShowFieldMessage("Package.Description", PackageCreatorFacade.GetLocalization("Required"));
                e.Result = false;
            }

            if (string.IsNullOrEmpty(package.TechnicalDetails))
            {
                this.ShowFieldMessage("Package.TechnicalDetails", PackageCreatorFacade.GetLocalization("Required"));
                e.Result = false;
            }

            if (this.EntityToken.Source != package.Name && PackageCreatorFacade.GetPackageNames().Contains(package.Name.Trim()))
            {
                this.ShowFieldMessage("Package.Name", PackageCreatorFacade.GetLocalization("Error.PackageAlreadyExist"));
                e.Result = false;
            }
            this.UpdateBinding("Package", package);

        }

        private void SaveCodeActivity_ExecuteCode(object sender, EventArgs e)
        {
            var package = this.GetBinding<PackageInformation>("Package");
            PackageCreatorFacade.SavePackageInformation(package, this.EntityToken.Source);
            SpecificTreeRefresher treeRefresher = this.CreateSpecificTreeRefresher();
            treeRefresher.PostRefreshMesseges(new PackageCreatorElementProviderEntityToken());

            this.UpdateBinding("PackageUrl", "123");

            this.SetSaveStatus(true);

        }
    }
}
